package com.timeriver.utils;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.logging.Logger;

/**
 * ZIP文件的上传与解压工具类
 */
public class ZIPFileUtil {

    private static Logger logger = Logger.getLogger(ZIPFileUtil.class.getName());

    /**
     * 递归删除路径下的文件
     * @param file
     */
    public static void deleteAllFiles(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files.length > 0 && files != null) {
                for (File f : files) {
                    deleteAllFiles(f);
                }
            }
        }

        logger.info("=====> 删除路径下文件：" + (file.delete() ? "成功" : "失败"));
    }

    /**
     * 递归解压压缩文件
     * @param file 待解压文件
     */
    public static void unzipFile(File file) {
        try (ZipArchiveInputStream inputStream = getZipFile(file)) {
            ArchiveEntry entry = null;
            while ((entry = inputStream.getNextEntry()) != null) {
                logger.info("=====> 递归解压文件：" + entry.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private static ZipArchiveInputStream getZipFile(File zipFile) throws Exception {
        return new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    }
}
